/*
 * GClubSeasonSummary.java
 * Created on 18 sept. 2008
 *
 * Glacéo Internet Platform
 * http://sourceforge.net/projects/chm/
 *
 * Copyright (c) 2005-2008, Jens and Jan Janke (VirtualHockey Project)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.hockeo.vhbase.data.dao.club;


import java.math.BigDecimal;
import java.util.Locale;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import com.hockeo.vhbase.conf.GRulesConstants;
import com.hockeo.vhbase.data.dao.GAbstractDao;
import com.hockeo.vhbase.data.dao.IGEntity;
import com.hockeo.vhbase.data.dao.season.GSeason;
import com.hockeo.vhbase.data.dao.standings.GECRStandings;
import com.hockeo.vhbase.data.helper.GContestRoundType;
import com.hockeo.vhbase.data.helper.GStandingsRankType;
import com.hockeo.vhbase.data.helper.GTranslationHelper;

/**
 * Summary representation of the most important results of a club in one season.
 *
 * @version $Id$
 * @author jjanke
 */
@Entity
@Table( name = "h_club_season_summary" )
public class GClubSeasonSummary extends GAbstractDao
{
  @Id
  @Column( name = "id" )
  @GeneratedValue( strategy = GenerationType.AUTO )
  private int                d_nId;

  @ManyToOne( fetch = FetchType.LAZY )
  @JoinColumn( name = "season_id" )
  private GSeason            d_season;

  @ManyToOne( fetch = FetchType.LAZY )
  @JoinColumn( name = "club_id" )
  private GClub              d_club;

  @Column( name = "ecr_rank" )
  private int                d_nECRRank     = 0;

  @Column( name = "ecr_points" )
  private BigDecimal         d_decECRPoints = new BigDecimal( "0.00" );

  @Column( name = "league_cat" )
  private int                d_nLeagueCategory;

  @Column( name = "league_rank" )
  private int                d_nLeagueRank;

  @Enumerated( EnumType.STRING )
  @Column( name = "league_rank_type" )
  private GStandingsRankType d_leagueRankType;

  @Column( name = "cup_rank" )
  private int                d_nCupRank;

  @Enumerated( EnumType.STRING )
  @Column( name = "cup_reached_round" )
  private GContestRoundType  d_cupReachedRound;

  @Column( name = "cup_won_rounds" )
  private int                d_nCupWonRounds;

  @Column( name = "cl_rank" )
  private int                d_nChampionsLeagueRank;

  @Enumerated( EnumType.STRING )
  @Column( name = "cl_reached_round" )
  private GContestRoundType  d_championsLeagueReachedRound;

  @Column( name = "wl_rank" )
  private int                d_nWorldLeagueRank;

  @Enumerated( EnumType.STRING )
  @Column( name = "wl_reached_round" )
  private GContestRoundType  d_worldLeagueReachedRound;

  /** Default parameterless constructor. */
  protected GClubSeasonSummary()
  {}

  /**
   * Creates a new club/season summary object based on the given detailed history records
   * from the different contests.
   *
   * @param leagueHistory detailed league results
   * @param cupHistory detailed national cup results
   * @param clHistory detailed champions league results
   * @param clHistory detailed world league results
   * @param ecr ECR standings for the given club and year
   */
  public GClubSeasonSummary( GClubLeagueHistoryRecord leagueHistory,
                             GClubCupHistoryRecord cupHistory,
                             GClubCLHistoryRecord clHistory,
                             GClubWLHistoryRecord wlHistory,
                             GECRStandings ecr )
  {
    assert leagueHistory != null : "Club/league history object is null.";
    assert cupHistory != null : "Club/cup history object is null.";

    d_season = leagueHistory.getSeason();
    d_club = cupHistory.getClub();

    d_nLeagueCategory = leagueHistory.getContest().getCategory();
    d_nLeagueRank = leagueHistory.getRank();
    d_leagueRankType = leagueHistory.getStandings().getRankType();

    d_nCupRank = cupHistory.getRank();
    d_cupReachedRound = cupHistory.getReachedRound();
    d_nCupWonRounds = cupHistory.getNumWonKOSeries();

    if ( clHistory != null )
    {
      d_nChampionsLeagueRank = clHistory.getRank();
      d_championsLeagueReachedRound = clHistory.getReachedRound();
    }

    if ( wlHistory != null )
    {
      d_nWorldLeagueRank = wlHistory.getRank();
      d_worldLeagueReachedRound = wlHistory.getReachedRound();
    }

    if ( ecr != null )
    {
      d_nECRRank = ecr.getRank();
      d_decECRPoints = ecr.getPointsS1();
    }
  }

  public GSeason getSeason()
  {
    return d_season;
  }

  public GClub getClub()
  {
    return d_club;
  }

  public int getECRRank()
  {
    return d_nECRRank;
  }

  public BigDecimal getECRPoints()
  {
    return d_decECRPoints;
  }

  public int getLeagueCategory()
  {
    return d_nLeagueCategory;
  }

  public int getLeagueRank()
  {
    return d_nLeagueRank;
  }

  public GStandingsRankType getLeagueRankType()
  {
    return d_leagueRankType;
  }

  public String getLeagueRankType( Locale locale )
  {
    return translate( "rankType_" + d_leagueRankType.name(), locale );
  }

  public int getCupRank()
  {
    return d_nCupRank;
  }

  public GContestRoundType getCupReachedRound()
  {
    return d_cupReachedRound;
  }

  public String getCupReachedRound( Locale locale )
  {
    if ( d_cupReachedRound != null )
      return getTranslatedRoundType( d_cupReachedRound, getClub().getAssociation().getNationalCupId(), locale );

    return null;
  }

  public int getCupWonRounds()
  {
    return d_nCupWonRounds;
  }

  public int getCLRank()
  {
    return d_nChampionsLeagueRank;
  }

  public GContestRoundType getCLReachedRound()
  {
    return d_championsLeagueReachedRound;
  }

  public String getCLReachedRound( Locale locale )
  {
    if ( d_championsLeagueReachedRound != null )
      return getTranslatedRoundType( d_championsLeagueReachedRound, GRulesConstants.SPECIAL_CONTEST_ID_CHAMPIONS_LEAGUE, locale );

    return null;
  }

  public int getWLRank()
  {
    return d_nWorldLeagueRank;
  }

  public GContestRoundType getWLReachedRound()
  {
    return d_worldLeagueReachedRound;
  }

  public String getWLReachedRound( Locale locale )
  {
    if ( d_worldLeagueReachedRound != null )
      return getTranslatedRoundType( d_worldLeagueReachedRound, GRulesConstants.SPECIAL_CONTEST_ID_WORLD_LEAGUE, locale );

    return null;
  }

  public int getId()
  {
    return d_nId;
  }

  /**
   * Returns the translated round type.
   *
   * @param roundType the round type to translate
   * @param strContestId the ID of the contest for which to translate the round type
   * @param locale the locale used for the translation
   * @return the translated round type as a string
   */
  private String getTranslatedRoundType( GContestRoundType roundType, String strContestId, Locale locale )
  {
    // First try with contest id
    //
    String strKey = "roundType_" + roundType.name() + "_" + strContestId;
    String str = GTranslationHelper.get().translate( strKey, locale );

    // If it does not exist, try with round type
    //
    if ( str.equals( strKey ) )
      str = GTranslationHelper.get().translate( "roundType_" + roundType.name(), locale );

    return str;
  }

  public Class<? extends IGEntity> getGEntityClass()
  {
    return GClubSeasonSummary.class;
  }

  /*
   * @see java.lang.Object#equals(java.lang.Object)
   */
  @Override
  public boolean equals( Object obj )
  {
    try
    {
      return d_nId == ( (GClubSeasonSummary) obj ).getId();
    }
    catch ( Exception ex ) // especially ClassCastException(s) may happen
    {
      return false;
    }
  }

  /*
   * @see java.lang.Object#hashCode()
   */
  @Override
  public int hashCode()
  {
    return d_nId;
  }

  /*
   * @see java.lang.Object#toString()
   */
  @Override
  public String toString()
  {
    return "Club/season summary record " + d_nId + " for " + d_club.getName() + " and season " + d_season.getName();
  }
}
